#连接数据库
from sqlalchemy import create_engine, Column, Integer, String, Text, Float, DateTime
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
# 创建基类
Base = declarative_base()

class User(Base):
    #表名
    __tablename__ = 'sys_user'
    """初始化字段如下：
        id
        username
        password
        real_name
        email
        phone
        status
        is_admin
        is_deleted
        create_time
        update_time
        last_login_time
        job_name
        remark
    """
    id = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(50))
    password = Column(String(100))
    real_name = Column(String(50))
    email = Column(String(100))
    phone = Column(String(20))
    status = Column(Integer)
    is_admin = Column(Integer)
    is_deleted = Column(Integer)
    create_time = Column(DateTime)
    update_time = Column(DateTime)
    last_login_time = Column(DateTime)
    job_name = Column(String(50))
    remark = Column(Text)
    def __repr__(self):
        return f"<User(id={self.id}, username='{self.username}', email='{self.email}')>"


class Menu(Base):
    __tablename__ = 'sys_menu'
    """初始化字段如下：id,parent_id,menu_name,path,icon,sort,permission,menu_type,component,visible,cache,system_id,short_name,remark,status
    """
    id = Column(Integer, primary_key=True, autoincrement=True)
    parent_id = Column(Integer)
    menu_name = Column(String(50))
    path = Column(String(100))
    icon = Column(String(50))
    sort = Column(Integer)
    permission = Column(String(100))
    menu_type = Column(Integer)
    component = Column(String(100))
    is_visible = Column(Integer)
    is_cache = Column(Integer)
    is_leaf = Column(Integer)
    system_id = Column(Integer)
    short_name = Column(String(50))
    remark = Column(Text)
    status = Column(Integer)
    create_date= Column(DateTime)
    update_date= Column(DateTime)


    def __repr__(self):
        return f"<Menu(id={self.id}, menu_name='{self.menu_name}', path='{self.path}')>"


class Role(Base):
    __tablename__ = 'sys_role'
    """初始化字段如下：id,role_id,role_name,role_key,sort,status,remark,is_deleted,create_time,update_time

    """
    id = Column(Integer, primary_key=True, autoincrement=True)
    role_name = Column(String(50))
    role_key = Column(String(50))
    sort = Column(Integer)
    status = Column(Integer)
    remark = Column(Text)
    is_deleted = Column(Integer)
    create_time = Column(DateTime)
    update_time = Column(DateTime)
    def __repr__(self):
        return f"<Role(id={self.id}, role_name='{self.role_name}', role_key='{self.role_key}')>"

class RolePermission(Base):
    __tablename__ ='sys_role_permission'
    """初始化字段如下：id,role_id,menu_id
    """
    id = Column(Integer, primary_key=True, autoincrement=True)
    role_id = Column(Integer)
    menu_id = Column(Integer)
    def __repr__(self):
        return f"<RolePermission(id={self.id}, role_id='{self.role_id}', menu_id='{self.menu_id}')>"

'''用户角色表'''
class UserRole(Base):
    __tablename__ ='sys_user_role'
    """初始化字段如下：id,user_id,role_id
    """
    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(Integer)
    role_id = Column(Integer)
    create_time= Column(DateTime)
    def __repr__(self):
        return f"<UserRole(id={self.id}, user_id='{self.user_id}', role_id='{self.role_id}')>"

'''用户登录日志表'''
class UserLoginLog(Base):
    __tablename__ ='sys_user_login_log'
    """字段：,id ,user_name ,login_ip ,login_location ,browser ,os ,login_tag ,operate_info ,status ,login_date ,create_date"""
    id = Column(Integer, primary_key=True, autoincrement=True)
    user_name = Column(String(50))
    login_ip = Column(String(50))
    login_location = Column(String(50))
    browser = Column(String(50))
    os = Column(String(50))
    login_tag = Column(String(50))
    operate_info = Column(Text)
    status = Column(Integer)
    login_date = Column(DateTime)
    create_date = Column(DateTime)
    def __repr__(self):
        return f"<UserLoginLog(id={self.id}, user_name='{self.user_name}', login_ip='{self.login_ip}')>"
